﻿using System;
using System.Linq;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace SPDeployer.Core
{
    class SiteManager
    {
        public SPSite Create(SPWebApplication webApplication, string url, string ownerLogin,
            string ownerEmail)
        {
            return
                webApplication.Sites.Add(url, ownerLogin, ownerEmail);
        }

        public SPSite Create(SPWebApplication webApplication, string url, string name, 
            string description, uint lcid, string webTemplate, string ownerLogin, 
            string ownerName, string ownerEmail)
        {
            return
                webApplication.Sites.Add(url, name, description, lcid, webTemplate, ownerLogin,
                                         ownerName, ownerEmail);
        }

        public bool Delete(SPWebApplication webApplication, string url)
        {
            try
            {
                webApplication.Sites.Delete(url);
                return true;
            }
            catch(Exception ex)
            {
                // TODO: Reportar exception.
                return false;
            }
        }

        public SPSite Get(SPWebApplication webApplication, string url)
        {
            return webApplication.Sites.SingleOrDefault(i => i.Url == url);
        }
    }
}
